package base.thread_JUC2.chapter03.demo07;

import java.util.List;
import java.util.concurrent.Exchanger;

public class Consumer implements Runnable{
    private List<String> list;
    private Exchanger<List<String>> exchanger;

    public Consumer(List<String> list, Exchanger<List<String>> exchanger) {
        this.list = list;
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        int cycle = 1;

        for (int i = 0; i < 10; i++) {
            System.out.printf("Consumer: Cycle %d\n",cycle);

            try {
                exchanger.exchange(list);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.printf("Consumer: %d\n",list.size());

            for (int j = 0; j < 5; j++) {
                System.out.printf("Consumer: %s\n", list.get(0));
                list.remove(0);
            }
            cycle++;
        }
    }
}
